<?php
class Subscribers
{
	private static $_instance = null;
	private $_db = null;
	
	private function __construct()
	{
		$this->_db = Yii::app()->db;
		$this->_db->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	
	public function getList(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(		
			);
			$args = array_merge($defaults, $args);
			
			$intStart = ($intPage-1)*$intLimit;
			
			$cmd = $this->_db->createCommand('SELECT SQL_CALC_FOUND_ROWS site_id, subscriber_id, subscriber_email, subscriber_name, subscriber_description, subscriber_created_date, subscriber_last_modified_date, subscriber_status
 											  FROM tbl_subscribers
											  ORDER BY subscriber_id DESC
											  LIMIT :intStart, :intLimit');
			$cmd->bindParam(":intStart",$intStart,PDO::PARAM_INT);
			$cmd->bindParam(":intLimit",$intLimit,PDO::PARAM_INT);	
			$arrResults = $cmd->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function get($id)
	{
		try{
			$strSQL = " SELECT *
						FROM tbl_subscribers
						WHERE subscriber_id=:id OR subscriber_email=:id";
			$cmd = $this->_db->createCommand($strSQL);
			$cmd->bindParam(":id",$id,PDO::PARAM_STR);
			return $arrResults = $cmd->queryRow();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function insert(array $args)
	{		
		try{
			$defaults = array(
				'site_id' => SITE, 
				'subscriber_email' => null, 
				'subscriber_name' => null, 
				'subscriber_description' => null, 
				'subscriber_created_date' =>  new CDbExpression('NOW()'), 
				'subscriber_status' => 0
			);
			$args = array_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			$cmd->insert('tbl_subscribers',$args);
			return Yii::app()->db->getLastInsertId();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function update($id, array $args)
	{		
		try{
			$defaults = array(
				'site_id' => SITE, 
				'subscriber_email' => null, 
				'subscriber_name' => null, 
				'subscriber_description' => null, 
				'subscriber_last_modified_date' => new CDbExpression('NOW()'), 
				'subscriber_status' => 0	
			);
			$args = array_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			return $cmd->update('tbl_subscribers',$args, 'subscriber_id=:id', array(':id'=>$id));
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function delete($id){
		try{
			$cmd = $this->_db->createCommand();
			return $cmd->delete('tbl_subscribers', 'subscriber_id=:id', array(':id'=>$id));			
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
}